package Function;

import FCRendering.FCRBattleOrderConstant;
import VWorldGameStudio.FCItem;
import com.google.gson.Gson;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import utils.FCDataSingles;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;

public class BuildFCItemByExcelMain {
    //TODO 注意当前的excel工具只支持.xls，不支持.xlsx，要转换一下
    private static final String ITEM_FILE = "./重装机兵V-数据-怪物.xls";
    private static final String TARGET_FILE = "./items/";
    private String[] tags = new String[]{"名称", "英文ID", "种类1", "种类2", "使用次数", "小类目", "重量"//0-6
            , "升级对象ID", "升级金额", "买入价", "卖出价", "容量", "属性名", "属性值"//7-13
            , "属性持续", "属性目标", "攻击值", "攻击目标", "暴击率", "破坏值", "破坏持续"//14-20
            , "破坏目标", "破甲值", "破甲目标", "回血值", "回血目标", "回血持续" //21-26
            , "回甲值", "回甲目标", "回甲持续", "甲值", "修理价格", "补满价格", "主炮穴"//27-33
            , "副炮穴", "SE穴", "弹仓数", "悬赏金", "关联怪物ID", "触发事件ID", "图片名"//34-40
            , "攻击动画ID", "描述特性", "命中率", "闪避率","物攻持续","速度提升","回体力值","关联坦克ID","命中值","回避值","迎击率","猎人"
    ,"机械","士兵","最大守备力","主穴通用","副穴通用"};//41-42
    private HashMap<String, Integer> tagsAndIndexMap = new HashMap<>();
    private HashMap<String, String> chToEnTypeMaps = new HashMap<>();
    private Gson gson = new Gson();
    Sheet sheet;

    public static void main(String[] args) {
        BuildFCItemByExcelMain main = new BuildFCItemByExcelMain();
        main.init();
    }

    public BuildFCItemByExcelMain() {
        chToEnTypeMaps.put("人类", "character");
        chToEnTypeMaps.put("坦克", "tank");
        chToEnTypeMaps.put("食物", "food");
        chToEnTypeMaps.put("饮料", "drug");
        chToEnTypeMaps.put("功能性工具", "tool");
        chToEnTypeMaps.put("投掷", "throw");
        chToEnTypeMaps.put("手持", "arm");
        chToEnTypeMaps.put("帽子", "hat");
        chToEnTypeMaps.put("上衣", "jacket");
        chToEnTypeMaps.put("防弹衣", "pants");
        chToEnTypeMaps.put("鞋子", "shoes");
        chToEnTypeMaps.put("护手套", "glove");
        chToEnTypeMaps.put("钥匙", "key");
        chToEnTypeMaps.put("剧情道具", "plotItem");
        chToEnTypeMaps.put("主炮", "gun1");
        chToEnTypeMaps.put("副炮", "gun2");
        chToEnTypeMaps.put("SE", "se");
        chToEnTypeMaps.put("发动机", "engine");
        chToEnTypeMaps.put("C芯片", "Cdevice");
        chToEnTypeMaps.put("底盘", "chassis");
        chToEnTypeMaps.put("雷达", "radar");
        chToEnTypeMaps.put("炮弹", "shell");
        chToEnTypeMaps.put("装甲片", "plate");
        chToEnTypeMaps.put("悬赏令", "wantedPic");
        chToEnTypeMaps.put("艺术品", "artwork");
        chToEnTypeMaps.put("书籍道具", "book");
        chToEnTypeMaps.put("全", "all");
        chToEnTypeMaps.put("能用", "1");
        chToEnTypeMaps.put("禁用", "0");
        chToEnTypeMaps.put("赏金猎人", "hunter");
        chToEnTypeMaps.put("酸性", "acid");
        chToEnTypeMaps.put("麻痹", "paralysis");
        chToEnTypeMaps.put("毒气", "gas");
        chToEnTypeMaps.put("火", "fire");
        chToEnTypeMaps.put("冷冻", "frozen");
        chToEnTypeMaps.put("睡眠", "sleep");
        chToEnTypeMaps.put("混乱", "confusion");
        chToEnTypeMaps.put("水泥", "cement");
        chToEnTypeMaps.put("烟雾", "fog");
        chToEnTypeMaps.put("打开", "1");
        chToEnTypeMaps.put("堵上", "0");
        chToEnTypeMaps.put("无限次", "-1");
        chToEnTypeMaps.put("单体", "1");
        chToEnTypeMaps.put("一组", "3");
        chToEnTypeMaps.put("全体", "8");
        chToEnTypeMaps.put("限制", "0");
        chToEnTypeMaps.put("通用", "1");
        chToEnTypeMaps.put("火炮", FCRBattleOrderConstant.WT_HuoPao);
        chToEnTypeMaps.put("枪", FCRBattleOrderConstant.WT_Qiang);
        chToEnTypeMaps.put("弹弓", FCRBattleOrderConstant.WT_DanGong);
        chToEnTypeMaps.put("徒手", FCRBattleOrderConstant.WT_TuShou);
        chToEnTypeMaps.put("刀", FCRBattleOrderConstant.WT_Dao);
        chToEnTypeMaps.put("旋镖", FCRBattleOrderConstant.WT_HuiXuanBiao);
    }

    public void init() {
        try {
            int ids = 1;
            File file = new File(ITEM_FILE);
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);
            sheet = wb.getSheet(1);
            boolean initKeyIndex = false;
            // sheet.getRows()返回该页的总行数
            for (int i = 0; i < sheet.getRows(); i++) {
                if (i == 1 || i == 0) {
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        Cell cell_1 = sheet.getCell(j, i);
                        if (cell_1 == null || cell_1.getContents().isEmpty()) {
                            continue;
                        }
                        String content = cell_1.getContents().trim();
                        for (String tag : tags) {
                            if (content.contains(tag)) {
                                System.out.println("TAG " + content + " j:" + j + " tag " + tag);
                                tagsAndIndexMap.put(tag, j);
                                break;
                            }
                        }
                    }
                    if (i == 1) {
                        initKeyIndex = true;
                    }
                }
                if (initKeyIndex) {
                    String chName = getCellNoMap(0, i);
                    if (chName == null) {
                        continue;
                    }
                    FCItem fcItem = new FCItem();
                    fcItem.setID(ids++);
                    fcItem.setChName(chName);
                    fcItem.setName(getCellNoMap(1, i));
                    fcItem.setCatalog(getCell(2, i));
                    fcItem.setType(getCell(3, i));
                    fcItem.setCanUseTimes(getCellInt(4, i, fcItem.getCanUseTimes()));
                    fcItem.setType2(getCellDef(5, i, "null"));
                    fcItem.setWeight(getCellDouble(6, i, 0));
                    fcItem.setUpgradeTarget(getCell(7, i));
                    fcItem.setUpgradeMoney(getCellInt(8, i, 0));
                    fcItem.setBuyInPrice(getCellInt(9, i, 0));
                    fcItem.setSellOutPrice(getCellInt(10, i, (int) (fcItem.getBuyInPrice() / 2)));
                    fcItem.setCapacity(getCellDouble(11, i, 0));
                    fcItem.setElements(getCellDef(12, i, "null"));
                    fcItem.setElementsValue(getCellInt(13, i, 0));
                    fcItem.setElementsRound(getCellInt(14, i, 0));
                    fcItem.setElementsNum(getCellInt(15, i, 1));
                    fcItem.setAttackHurt(getCellInt(16, i, 0));
                    fcItem.setAttackNum(getCellInt(17, i, 1));
                    fcItem.setCrit(getCellDouble(18, i, 0));
                    fcItem.setSubdefense(getCellInt(19, i, 0));
                    fcItem.setSubdefenseRound(getCellInt(20, i, 0));
                    fcItem.setSubdefenseNum(getCellInt(21, i, 1));
                    fcItem.setDesdefense(getCellInt(22, i, (int)(fcItem.getAttackHurt()*1.25)));
                    fcItem.setDesdefenseNum(getCellInt(23, i, 1));
                    fcItem.setBloodAdd(getCellInt(24, i, 0));
                    fcItem.setBloodNum(getCellInt(25, i, 1));
                    fcItem.setBloodRound(getCellInt(26, i, 0));
                    int defense = getCellInt(27, i, 0);
                    if(fcItem.getCatalog().equals("tank")){
                        defense += defense;
                    }
                    fcItem.setDefenseAdd(defense);//甲值，要是是坦克用品的话，要x2,因为计算破损方式不同
                    fcItem.setDefenseNum(getCellInt(28, i, 1));
                    fcItem.setDefernseRound(getCellInt(29, i, 0));
                    fcItem.setDefenseAdd(getCellInt(30, i, 0));
                    fcItem.setRepairPrice(getCellInt(31, i, 0));
                    fcItem.setFillServicePrice(getCellInt(32, i, 2));
                    fcItem.getCannonHoles()[0] = getCellInt(33, i, 0);
                    fcItem.getCannonHoles()[1] = getCellInt(33, i, 0);
                    fcItem.getCannonHoles()[2] = getCellInt(34, i, 0);
                    fcItem.setShellCaseNum(getCellInt(36, i, 0));
                    fcItem.setWantedPrice(getCellInt(37, i, 0));
                    fcItem.setRelatedMonster(getCellDef(38, i, "null"));
                    fcItem.setEventID(getCellDef(39, i, "null"));
                    String url = getCellDef(40, i, "default");
                    if (url != null) {
                        if (!url.contains("png")) {
                            url = url + ".png";
                        }
                        fcItem.setPngUrl("./material/png/item/" + url);
                    }
                    fcItem.setAttackAnimation(getCellDef(41, i, "null"));
                    fcItem.setDepict(getCellDef(42, i, "无"));
                    //如果是属于武器攻击类的话，默认是100，否则是0
                    fcItem.setHitRate(getCellInt(43, i, 0));

                    fcItem.setDodgeRateAdd(getCellInt(44, i, 0));
                    fcItem.setAttackRound(getCellInt(45, i, 1));
                    fcItem.setSpeedAdd(getCellInt(46, i, 0));
                    fcItem.setPhysicalAdd(getCellInt(47, i, 0));
                    fcItem.setRelateTankId(getCellDef(48, i, "null"));
                    fcItem.setHitValueForCDevice(getCellInt(49,i ,0));
                    fcItem.setDodgeValueForCDevice(getCellInt(50,i ,0));
                    fcItem.setCounteRateForCDevice(getCellInt(51,i ,0));
                    String enableOccupation = "";
                    if(getCellInt(52,i,1) == 1){
                        enableOccupation += "hunter";
                    }
                    if(getCellInt(53,i,1) == 1){
                        enableOccupation += "machinist";
                    }
                    if(getCellInt(54,i,1) == 1){
                        enableOccupation += "warrior";
                    }
                    fcItem.setEnableOccupation(enableOccupation);
                    fcItem.setMaxDefenseUpgrade(getCellInt(55,i ,0));
                    fcItem.setGun1HoleEnable(getCellInt(56,i ,0)==1);
                    fcItem.setGun2HoleEnable(getCellInt(57,i ,0)==1);
                    System.out.println("---->" + gson.toJson(fcItem));
                    FCDataSingles.getWriter().writeItemData(fcItem);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getIndex(int index) {
        Integer ret = tagsAndIndexMap.get(tags[index]);
        //   System.out.println("getIndex "+index+" "+ret+"  "+tags[index]);
        if (ret == null) {
            return index;
        }
        return ret;
    }

    private String getEnWords(String c) {
        if (chToEnTypeMaps.containsKey(c)) {
            return chToEnTypeMaps.get(c);
        }
        return c;
    }
    public String getCellNoMap(int index, int i) {
        index = getIndex(index);
        if (index < 0) {
            System.out.println("getCell index < 0");
            return null;
        }

        Cell cell = sheet.getCell(index, i);
        if (cell == null || cell.getContents() == null || cell.getContents().isEmpty()) {
            return null;
        }
        return cell.getContents().trim();
    }
    public String getCell(int index, int i) {
        index = getIndex(index);
        if (index < 0) {
            System.out.println("getCell index < 0");
            return null;
        }

        Cell cell = sheet.getCell(index, i);
        if (cell == null || cell.getContents() == null || cell.getContents().isEmpty()) {
            return null;
        }
        return getEnWords(cell.getContents().trim());
    }

    public String getCellDef(int index, int i, String def) {
        String d = getCell(index, i);
        if (d != null) {
            return getEnWords(d);
        }
        return def;
    }

    public int getCellInt(int index, int i, int def) {
        String d = getCell(index, i);
        if (d != null) {
            return Integer.parseInt(d);
        }
        return def;
    }

    public double getCellDouble(int index, int i, double def) {
        String d = getCell(index, i);
        if (d != null) {
            return Double.parseDouble(d);
        }
        return def;
    }
}
